package com.hspedu.furn.service.serviceImpl;

import com.hspedu.furn.bean.Furn;
import com.hspedu.furn.bean.FurnExample;
import com.hspedu.furn.dao.FurnMapper;
import com.hspedu.furn.service.FurnService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/9/24 13:48
 */
@Service        // 扫描ioc容器
public class FurnServiceImpl implements FurnService {
    @Resource
    private FurnMapper furnMapper;      // 注入furnMapper

    @Override
    public void save(Furn furn) {        // 增加家居信息
        furnMapper.insertSelective(furn);   // 调用dao层，操作数据库
    }

    @Override
    public void delete(Integer id) {        // 删除家居信息
        furnMapper.deleteByPrimaryKey(id);  // 调用dao层，操作数据库
    }

    @Override
    public void update(Furn furn) {     // 修改家居信息
        furnMapper.updateByPrimaryKeySelective(furn);    // 调用dao层，操作数据库
    }

    @Override
    public Furn selectFurnById(Integer id) {    // 根据家居id查找家居信息
        Furn furn = furnMapper.selectByPrimaryKey(id);  // 调用dao层，操作数据库
        return furn;
    }

    @Override
    public List<Furn> selectAllFurn() {     // 查询furn表中的所有家居信息
        List<Furn> furns = furnMapper.selectAllFurn();
        return furns;
    }

    @Override
    public List<Furn> selectAllByName(String name) {    // 根据name进行模糊查询
        FurnExample furnExample = new FurnExample();
        FurnExample.Criteria criteria = furnExample.createCriteria();
        // 该对象可以设置查询条件
        if (StringUtils.hasText(name)) {
            // 如果name有值则设置查询条件，否则查询条件为空仍然显示所有家居
            criteria.andNameLike("%" + name + "%"); // 根据名字进行模糊查询
        }
        List<Furn> furns = furnMapper.selectByExample(furnExample);
        return furns;
    }
}
